<?php


namespace app\admin\model;


class Alipay
{

    private  $aop;
    public function __construct(){
        require_once  config('public.vendor_url').'/alipay/AopClient.php';
        $this->aop =  new \AopClient();
        $alipay = getRedisDb('alipay');
        $this->aop->gatewayUrl = $alipay['gatewayUrl'];
        $this->aop->appId = $alipay['appId'];
        $this->aop->rsaPrivateKey = $alipay['rsaPrivateKey'];
        $this->aop->postCharset = "UTF-8";
        $this->aop->apiVersion = '1.0';
        $this->aop->format = "json";
        $this->aop->signType = $alipay['signType'];
        $this->aop->alipayrsaPublicKey = $alipay['rsaPublicKey'];
    }

    /**
     * @param $order_number string 订单号
     * @param $alipayName string 支付宝帐号
     * @param $alipayUseranme string 支付宝姓名
     * @param $payName string 支付方姓名
     * @param $remark string 备注
     * @param $money string 备注
     * @throws \Exception
     */
    public function  transfer($order_number,$money,$alipayName,$alipayUseranme,$payName,$remark=''){
        require_once  config('public.vendor_url').'/alipay/request/AlipayFundTransToaccountTransferRequest.php';
        $request = new \AlipayFundTransToaccountTransferRequest();
        $request->setBizContent("{" .
            "\"out_biz_no\":\"".$order_number."\"," .//商户生成订单号
            "\"payee_type\":\"ALIPAY_LOGONID\"," .//收款方支付宝账号类型
            "\"payee_account\":\"".$alipayName."\"," .//收款方账号
            "\"amount\":\"".$money."\"," .//总金额
            "\"payer_show_name\":\"".$payName."\"," .//付款方账户
            "\"payee_real_name\":\"".$alipayUseranme."\"," .//收款方姓名
            "\"remark\":\"".$remark."\"" .//转账备注
            "}");
        $result = $this->aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            return ['status'=>true,'msg'=>'success','order_id'=>$result->$responseNode->order_id];
        } else {
            return ['status'=>false,'msg'=>$result->$responseNode->sub_msg];
        }
    }
    public function query($orderNumber){
        require_once  config('public.vendor_url').'/alipay/request/AlipayFundTransOrderQueryRequest.php';
        $request = new \AlipayFundTransOrderQueryRequest ();
        $request->setBizContent("{" .
            "\"out_biz_no\":\"".$orderNumber."\"" .
            " }");
        $result = $this->aop->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        print_v($result->$responseNode);
        if(!empty($resultCode)&&$resultCode == 10000){
            $res_arr['code'] = '00';
            $res_arr['data'] = $result;
        } else {
            $res_arr['code'] = '-1';
        }
        return $res_arr;
    }
}